package TouristManagementBO.Site;

import java.util.Properties;

import TouristManagement.*;

public class SiteServer {
	
	VisitManagement leVisitManagement;
	ServerNamingTourist leServerNamingTourist;
	
	public SiteServer(){
	}
	
	public void initServer(){
		this.connectToServerNamingTourist();
		this.connectToVisitManagement();
	}
	
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void connectToVisitManagement(){
		//init VisitManagement server
		try {
			
			Properties props = new Properties();
	        props.put("org.omg.CORBA.ORBInitialPort", "2001");
	        props.put("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
	        
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomVisitManagement(),"");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantVisitManagement = nameRoot.resolve(nameToFind);
//	        System.out.println(orb.object_to_string(distantVisitManagement));

	        leVisitManagement = VisitManagementHelper.narrow(distantVisitManagement);

	        System.out.println("Objet VisitManagement connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void startSite(){
		short[] listeSite = leVisitManagement.getIdSite();				
		for (short i:listeSite){
			SiteServerThread leSite = new SiteServerThread(i);	
			leSite.initServer();
			leSite.start();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {		
		SiteServer leServerSite = new SiteServer();
		leServerSite.initServer();		
		leServerSite.startSite();
	}
		
}
